/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import logic.simplex.solver.InfinitySolutionException;
import presentation.controller.ControlPanelSearchSubjects;
import presentation.controller.SchedulePanelControl;
import vo.SubjectVO;

/**
 *
 * @author Joanna 8a
 */
public class PanelSearchSubjects extends javax.swing.JPanel {

    public static final int CREATE_SCHEDULE = 1;
    public static final int UPDATE_SCHEDULE = 2;

    /**
     * Creates new form PanelMaterias
     */
    public PanelSearchSubjects(AuxiliarFrame parent, int option) {
        this.parent = parent;
        initComponents();
        if (option == UPDATE_SCHEDULE) {
            List<SubjectVO> subjects = new ControlPanelSearchSubjects().getSchedule();
            if (!subjects.isEmpty()) {
                DefaultTableModel mod = (DefaultTableModel) jTableMySubjects.getModel();
                Object[] fila = new Object[3];
                Integer credits = 0;
                for (SubjectVO sub : subjects) {
                    if (!isAdded(sub.getCode())) {
                        fila[0] = sub.getCode() / 100;
                        fila[1] = sub.getName();
                        fila[2] = sub.getCredits();
                        credits += sub.getCredits();
                        mod.addRow(fila);
                    }
                }
                jLabelNrCredits.setText(credits.toString());
            }
            parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Search.png")).getImage());
            jButtonCreateSchedule.setText("Deseo modificar mi horario");
            jButtonGenerateSchedule.setText("Modificar Horario Automáticamente");
        } else if (option == CREATE_SCHEDULE) {
            parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Search.png")).getImage());
            jButtonCreateSchedule.setText("Deseo armar mi horario");
            jButtonGenerateSchedule.setText("Generar Horario Automáticamente");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupTypeSearch = new javax.swing.ButtonGroup();
        jButtonGenerateSchedule = new javax.swing.JButton();
        jButtonCreateSchedule = new javax.swing.JButton();
        jPanelMySubjects = new javax.swing.JPanel();
        jLabelCredits = new javax.swing.JLabel();
        jLabelNrCredits = new javax.swing.JLabel();
        jScrollPaneMySubjects = new javax.swing.JScrollPane();
        jTableMySubjects = new javax.swing.JTable();
        jPanelSearchSubjects = new javax.swing.JPanel();
        jLabelTypeSearch = new javax.swing.JLabel();
        jRadioButtonByCode = new javax.swing.JRadioButton();
        jRadioButtonByName = new javax.swing.JRadioButton();
        jTextFieldSearchSubject = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jPanelFoundSubjects = new javax.swing.JPanel();
        jScrollPaneFoundSubjects = new javax.swing.JScrollPane();
        jTableFoundSubjects = new javax.swing.JTable();
        jButtonAddSubject = new javax.swing.JButton();
        jButtonRemoveSubject = new javax.swing.JButton();

        jButtonGenerateSchedule.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonGenerateSchedule.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Application.png"))); // NOI18N
        jButtonGenerateSchedule.setText("Generar Horario Automáticamente");
        jButtonGenerateSchedule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGenerateScheduleActionPerformed(evt);
            }
        });

        jButtonCreateSchedule.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonCreateSchedule.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Calendar.png"))); // NOI18N
        jButtonCreateSchedule.setText("Deseo armar mi horario");
        jButtonCreateSchedule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCreateScheduleActionPerformed(evt);
            }
        });

        jPanelMySubjects.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Mis materias", javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(153, 0, 0))); // NOI18N

        jLabelCredits.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jLabelCredits.setText("Total créditos: ");

        jLabelNrCredits.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabelNrCredits.setText("0");

        jScrollPaneMySubjects.setBorder(null);

        jTableMySubjects.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jTableMySubjects.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Materia", "Créditos"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableMySubjects.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMySubjectsMouseClicked(evt);
            }
        });
        jScrollPaneMySubjects.setViewportView(jTableMySubjects);

        javax.swing.GroupLayout jPanelMySubjectsLayout = new javax.swing.GroupLayout(jPanelMySubjects);
        jPanelMySubjects.setLayout(jPanelMySubjectsLayout);
        jPanelMySubjectsLayout.setHorizontalGroup(
            jPanelMySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMySubjectsLayout.createSequentialGroup()
                .addGroup(jPanelMySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneMySubjects)
                    .addGroup(jPanelMySubjectsLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelCredits)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelNrCredits, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelMySubjectsLayout.setVerticalGroup(
            jPanelMySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMySubjectsLayout.createSequentialGroup()
                .addComponent(jScrollPaneMySubjects, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelMySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCredits)
                    .addComponent(jLabelNrCredits))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelSearchSubjects.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Busqueda de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(0, 102, 51))); // NOI18N

        jLabelTypeSearch.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabelTypeSearch.setText("Buscar por");

        buttonGroupTypeSearch.add(jRadioButtonByCode);
        jRadioButtonByCode.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jRadioButtonByCode.setSelected(true);
        jRadioButtonByCode.setText("Código");

        buttonGroupTypeSearch.add(jRadioButtonByName);
        jRadioButtonByName.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jRadioButtonByName.setText("Nombre");

        jTextFieldSearchSubject.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jButtonSearch.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Search.png"))); // NOI18N
        jButtonSearch.setText("Buscar");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelSearchSubjectsLayout = new javax.swing.GroupLayout(jPanelSearchSubjects);
        jPanelSearchSubjects.setLayout(jPanelSearchSubjectsLayout);
        jPanelSearchSubjectsLayout.setHorizontalGroup(
            jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelSearchSubjectsLayout.createSequentialGroup()
                .addGap(0, 136, Short.MAX_VALUE)
                .addGroup(jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSearchSubjectsLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jTextFieldSearchSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelSearchSubjectsLayout.createSequentialGroup()
                            .addComponent(jLabelTypeSearch)
                            .addGap(404, 404, 404))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelSearchSubjectsLayout.createSequentialGroup()
                            .addComponent(jRadioButtonByCode)
                            .addGap(121, 121, 121)
                            .addComponent(jRadioButtonByName)
                            .addGap(71, 71, 71)
                            .addComponent(jButtonSearch)
                            .addGap(128, 128, 128)))))
        );
        jPanelSearchSubjectsLayout.setVerticalGroup(
            jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSearchSubjectsLayout.createSequentialGroup()
                .addGroup(jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelTypeSearch)
                    .addGroup(jPanelSearchSubjectsLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanelSearchSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonSearch)
                            .addComponent(jRadioButtonByName)
                            .addComponent(jRadioButtonByCode))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTextFieldSearchSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );

        jPanelFoundSubjects.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultados Búsqueda", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(0, 0, 204))); // NOI18N

        jTableFoundSubjects.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jTableFoundSubjects.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Materia", "Creditos"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableFoundSubjects.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableFoundSubjectsMouseClicked(evt);
            }
        });
        jScrollPaneFoundSubjects.setViewportView(jTableFoundSubjects);

        javax.swing.GroupLayout jPanelFoundSubjectsLayout = new javax.swing.GroupLayout(jPanelFoundSubjects);
        jPanelFoundSubjects.setLayout(jPanelFoundSubjectsLayout);
        jPanelFoundSubjectsLayout.setHorizontalGroup(
            jPanelFoundSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneFoundSubjects, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
        );
        jPanelFoundSubjectsLayout.setVerticalGroup(
            jPanelFoundSubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFoundSubjectsLayout.createSequentialGroup()
                .addComponent(jScrollPaneFoundSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jButtonAddSubject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Arrow3 Down.png"))); // NOI18N
        jButtonAddSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddSubjectActionPerformed(evt);
            }
        });

        jButtonRemoveSubject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Arrow3 Up.png"))); // NOI18N
        jButtonRemoveSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveSubjectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addComponent(jButtonCreateSchedule)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                .addComponent(jButtonGenerateSchedule)
                .addGap(100, 100, 100))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanelFoundSubjects, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanelSearchSubjects, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanelMySubjects, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(246, 246, 246)
                        .addComponent(jButtonAddSubject)
                        .addGap(45, 45, 45)
                        .addComponent(jButtonRemoveSubject)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelSearchSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelFoundSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonAddSubject)
                    .addComponent(jButtonRemoveSubject))
                .addGap(10, 10, 10)
                .addComponent(jPanelMySubjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCreateSchedule)
                    .addComponent(jButtonGenerateSchedule))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGenerateScheduleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateScheduleActionPerformed
        // TODO terminar código:
        
        DefaultTableModel model = (DefaultTableModel) jTableMySubjects.getModel();
        Integer[] codigos = new Integer[model.getRowCount()];
        for (int i = 0; i < model.getRowCount(); i++) {
            codigos[i] = (Integer) model.getValueAt(i, 0);
        }
        
        //Debería hacer una verificación de nombre para la actualización?!?
        if (jButtonGenerateSchedule.getText().equals("Generar Horario Automáticamente")) {
            try {
                ArrayList<SubjectVO> generateSchedule = new SchedulePanelControl().generateSchedule(codigos, CREATE_SCHEDULE, parent, this);
                parent.cambiarPanel(new PanelCreateSchedule(parent, this, generateSchedule, CREATE_SCHEDULE));
            } catch (InfinitySolutionException ex) {
                Logger.getLogger(PanelSearchSubjects.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            }
        } else {
            try {
                ArrayList<SubjectVO> generateSchedule = new SchedulePanelControl().generateSchedule(codigos, UPDATE_SCHEDULE, parent, this);
                parent.cambiarPanel(new PanelCreateSchedule(parent, this, generateSchedule, UPDATE_SCHEDULE));
            } catch (InfinitySolutionException ex) {
                Logger.getLogger(PanelSearchSubjects.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButtonGenerateScheduleActionPerformed

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        List<SubjectVO> subjects = new ArrayList<>();
        if (jRadioButtonByCode.isSelected()) {
            if (new ViewHelper().validateNumbers(jTextFieldSearchSubject.getText())) {
                subjects = new ControlPanelSearchSubjects().searchSubjects(Integer.parseInt(jTextFieldSearchSubject.getText()), jRadioButtonByCode.isSelected());
            } else {
                JOptionPane.showMessageDialog(this, "Al buscar por codigo debe ingresar solo numeros!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } else {
            if (new ViewHelper().validateString(jTextFieldSearchSubject.getText())) {
                subjects = new ControlPanelSearchSubjects().searchSubjects(jTextFieldSearchSubject.getText(), jRadioButtonByCode.isSelected());
            } else {
                JOptionPane.showMessageDialog(this, "Al buscar por nombre debe ingresar solo letras!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        if (!subjects.isEmpty()) {
            DefaultTableModel mod = (DefaultTableModel) jTableFoundSubjects.getModel();
            Object[] fila = new Object[3];
            for (SubjectVO sub : subjects) {
                if (!isAdded(sub.getCode())) {
                    fila[0] = sub.getCode();
                    fila[1] = sub.getName();
                    fila[2] = sub.getCredits();
                    mod.addRow(fila);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "No se ha encontrado ninguna materia!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButtonSearchActionPerformed

    private void jTableFoundSubjectsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableFoundSubjectsMouseClicked
        filaBusqueda = jTableFoundSubjects.rowAtPoint(evt.getPoint());
    }//GEN-LAST:event_jTableFoundSubjectsMouseClicked

    private void jTableMySubjectsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMySubjectsMouseClicked
        filaMisMaterias = jTableMySubjects.rowAtPoint(evt.getPoint());
    }//GEN-LAST:event_jTableMySubjectsMouseClicked

    private void jButtonAddSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddSubjectActionPerformed
        DefaultTableModel mod1 = (DefaultTableModel) jTableFoundSubjects.getModel();
        DefaultTableModel mod2 = (DefaultTableModel) jTableMySubjects.getModel();
        if (mod1.getRowCount() > 0) {
            Object[] row = new Object[3];
            row[0] = mod1.getValueAt(filaBusqueda, 0);
            row[1] = mod1.getValueAt(filaBusqueda, 1);
            row[2] = mod1.getValueAt(filaBusqueda, 2);
            mod1.removeRow(filaBusqueda);
            mod2.addRow(row);
            Integer creditos = Integer.parseInt(jLabelNrCredits.getText()) + (Integer) row[2];
            jLabelNrCredits.setText(creditos.toString());
        }
    }//GEN-LAST:event_jButtonAddSubjectActionPerformed

    private void jButtonRemoveSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveSubjectActionPerformed
        DefaultTableModel mod2 = (DefaultTableModel) jTableFoundSubjects.getModel();
        DefaultTableModel mod1 = (DefaultTableModel) jTableMySubjects.getModel();
        if (mod1.getRowCount() > 0) {
            Object[] row = new Object[3];
            row[0] = mod1.getValueAt(filaMisMaterias, 0);
            row[1] = mod1.getValueAt(filaMisMaterias, 1);
            row[2] = mod1.getValueAt(filaMisMaterias, 2);
            mod1.removeRow(filaMisMaterias);
            mod2.addRow(row);
            Integer creditos = Integer.parseInt(jLabelNrCredits.getText()) - (Integer) row[2];
            jLabelNrCredits.setText(creditos.toString());
        }
    }//GEN-LAST:event_jButtonRemoveSubjectActionPerformed

    private void jButtonCreateScheduleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateScheduleActionPerformed

        DefaultTableModel model = (DefaultTableModel) jTableMySubjects.getModel();
        Integer[] codigos = new Integer[model.getRowCount()];
        for (int i = 0; i < model.getRowCount(); i++) {
            codigos[i] = (Integer) model.getValueAt(i, 0);
        }
        if (jButtonCreateSchedule.getText().equals("Deseo armar mi horario")) {
            new SchedulePanelControl().armarHorario(codigos, CREATE_SCHEDULE, parent, this);
        } else {
            new SchedulePanelControl().armarHorario(codigos, UPDATE_SCHEDULE, parent, this);
        }

    }//GEN-LAST:event_jButtonCreateScheduleActionPerformed

    private boolean isAdded(int codigo) {
        DefaultTableModel mod1 = (DefaultTableModel) jTableFoundSubjects.getModel();
        DefaultTableModel mod2 = (DefaultTableModel) jTableMySubjects.getModel();
        for (int i = 0; i < mod1.getRowCount(); i++) {
            if ((int) mod1.getValueAt(i, 0) == codigo) {
                return true;
            }
        }
        for (int i = 0; i < mod2.getRowCount(); i++) {
            if ((int) mod2.getValueAt(i, 0) == codigo) {
                return true;
            }
        }
        return false;
    }
    private int filaBusqueda;
    private int filaMisMaterias;
    private AuxiliarFrame parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupTypeSearch;
    private javax.swing.JButton jButtonAddSubject;
    private javax.swing.JButton jButtonCreateSchedule;
    private javax.swing.JButton jButtonGenerateSchedule;
    private javax.swing.JButton jButtonRemoveSubject;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabelCredits;
    private javax.swing.JLabel jLabelNrCredits;
    private javax.swing.JLabel jLabelTypeSearch;
    private javax.swing.JPanel jPanelFoundSubjects;
    private javax.swing.JPanel jPanelMySubjects;
    private javax.swing.JPanel jPanelSearchSubjects;
    private javax.swing.JRadioButton jRadioButtonByCode;
    private javax.swing.JRadioButton jRadioButtonByName;
    private javax.swing.JScrollPane jScrollPaneFoundSubjects;
    private javax.swing.JScrollPane jScrollPaneMySubjects;
    private javax.swing.JTable jTableFoundSubjects;
    private javax.swing.JTable jTableMySubjects;
    private javax.swing.JTextField jTextFieldSearchSubject;
    // End of variables declaration//GEN-END:variables
}
